﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Widget/SElementUIMessage.h"
#include "SlateOptMacros.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SElementUIMessage::Construct(const FArguments& InArgs)
{
	MySlateCurveSequence = FCurveSequence(0.f, 1.f, ECurveEaseFunction::QuadInOut);
	SetColorAndOpacity(TAttribute<FLinearColor>::Create(TAttribute<FLinearColor>::FGetter::CreateSP(this, &SElementUIMessage::SlateColorChanged)));
	
	ChildSlot
	[
		SAssignNew(BorderBox, SBorder)
        	.Padding(0.0f)
        	.HAlign(HAlign_Center)
        	.VAlign(VAlign_Center)
        	.ColorAndOpacity(this, &SElementUIMessage::GetColorAndOpacity)
		[
			InArgs._Content.Widget
		]
	];
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SElementUIMessage::ShowMessage()
{
	if (!MySlateCurveSequence.IsAtEnd())
	{
		if (MySlateCurveSequence.IsInReverse() && MySlateCurveSequence.IsPlaying())
		{
			MySlateCurveSequence.Reverse();
		}
		else if (!MySlateCurveSequence.IsPlaying())
		{
			MySlateCurveSequence.Play(this->AsShared());
		}
	}
}

void SElementUIMessage::HideMessage()
{
	if (!MySlateCurveSequence.IsAtStart())
	{
		if (!MySlateCurveSequence.IsInReverse() && MySlateCurveSequence.IsPlaying())
		{
			MySlateCurveSequence.Reverse();
		}
		else if (!MySlateCurveSequence.IsPlaying())
		{
			MySlateCurveSequence.PlayReverse(this->AsShared());
		}
	}
}

void SElementUIMessage::SetContentPadding(const FMargin& value)
{
	BorderBox->SetPadding(value);
}

void SElementUIMessage::SetHAlign(EHorizontalAlignment value)
{
	BorderBox->SetHAlign(value);
}

void SElementUIMessage::SetVAlign(EVerticalAlignment value)
{
	BorderBox->SetVAlign(value);
}

void SElementUIMessage::SetContent(TSharedRef<SWidget> InContent)
{
	BorderBox->SetContent(InContent);
}

FLinearColor SElementUIMessage::SlateColorChanged()
{
	SetRenderTransform(TOptional<FSlateRenderTransform>(FSlateRenderTransform(1.0,FVector2D(0,MySlateCurveSequence.GetLerp() * 60))));
	return FLinearColor(1, 1, 1, MySlateCurveSequence.GetLerp());
}
